<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <!-- 黑马logo -->
        <img src="../assets/ow-small.jpg"
             alt="">
        <!-- 顶部标题 -->
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info"
                 @click="logout"> 退出 </el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 宽度根据是否折叠进行设置 -->
      <el-aside :width="isCollapse ? '64px':'200px'">
        <!-- 折叠按钮 -->
        <div class="toggle-button"
             @click="isCollapse=!isCollapse">|||</div>
        <!-- 侧边栏菜单 -->
        <el-menu background-color="#313131"
                 text-color="#fff"
                 active-text-color="#409EFF"
                 :unique-opened="true"
                 :collapse="isCollapse"
                 :collapse-transition="false"
                 :router="true"
                 :default-active="activePath">

          <!-- 一级菜单 -->
          <el-submenu :index="item.id.toString()"
                      v-for="item in menuList"
                      :key="item.id">
            <!-- 一级菜单模板 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="item.iconclass"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级子菜单 -->
            <el-menu-item :index="'/'+subIMenu.path.toString()"
                          v-for="subIMenu in item.children"
                          :key="subIMenu.id"
                          @click="changePath('/'+subIMenu.path)">
              <!-- 二级菜单模板 -->
              <template slot="title">
                <!-- 图标 -->
                <i :class="subIMenu.iconclass"></i>
                <!-- 文本 -->
                <span>{{subIMenu.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--右侧内容区 主体结构 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { getMenusAPI } from '@/api'
export default {
  data () {
    return {
      menuList: [],//菜单数组
      isCollapse: false,//是否折叠菜单
      activePath: ""
    }
  },
  methods: {
    logout () {
      if (!confirm("确定退出么?")) return;
      window.sessionStorage.clear(); //删除token
      this.$router.replace("/login"); //使用replace跳转页面 没有此次跳转的历史记录 也就无法后退回来
    },
    changePath (aPath) {
      window.sessionStorage.setItem("activePath", aPath);
      this.activePath = aPath
    }
  },
  // 组件入口函数
  async created () {
    // 请求菜单数据
    let { data: res } = await getMenusAPI();
    // 如果请求失败 则提示失败
    if (res.meta.status !== 200) return this.$msg.error(res.meta.msg);
    // 如果请求成功  则将数据挂载给data变量
    this.menuList = res.data
    // console.log(this.menuList);
    //根据sessionStorage  设置默认选中菜单
   this.activePath = window.sessionStorage.getItem("activePath")
  }
};
</script>

<style lang='less' scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color:#313131;
  display: flex;
  justify-content: space-between;
  padding-left: 0px;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: 0px;
  }
}
.el-main {
  background-color: #eaedf1;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 1px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>